<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 英文输入法（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p><strong>主管</strong>期望你来实现英文输入法单词联想功能。</p> 
<p>需求如下&#xff1a;</p> 
<ul><li>依据用户输入的单词前缀&#xff0c;从已输入的英文语句中联想出用户想输入的单词&#xff0c;按字典序输出联想到的单词序列&#xff0c;</li><li>如果联想不到&#xff0c;请输出用户输入的单词前缀。</li></ul> 
<p>注意&#xff1a;</p> 
<ol><li>英文单词联想时&#xff0c;区分大小写</li><li>缩略形式如”don’t”&#xff0c;判定为两个单词&#xff0c;”don”和”t”</li><li>输出的单词序列&#xff0c;不能有重复单词&#xff0c;且只能是英文单词&#xff0c;不能有标点符号</li></ol> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入为两行。</p> 
<p>首行输入一段由英文单词word和标点符号组成的语句str&#xff1b;</p> 
<p>接下来一行为一个英文单词前缀pre。</p> 
<ul><li>0 &lt; word.length() &lt;&#61; 20</li><li>0 &lt; str.length &lt;&#61; 10000</li><li>0 &lt; pre &lt;&#61; 20</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出符合要求的单词序列或单词前缀&#xff0c;存在多个时&#xff0c;单词之间以单个空格分割</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">I love you<br /> He</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">He</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>从用户已输入英文语句”I love you”中提炼出“I”、“love”、“you”三个单词&#xff0c;接下来用户输入“He”&#xff0c;</p> <p>从已输入信息中无法联想到任何符合要求的单词&#xff0c;因此输出用户输入的单词前缀。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;"> <p>The furthest distance in the world, Is not between life and death, But when I stand in front of you, Yet you don&#39;t know that I love you.</p> <p>f</p> </td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">front furthest</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">从用户已输入英文语句”The furthestdistance in the world, Is not between life and death, But when I stand in frontof you, Yet you dont know that I love you.”中提炼出的单词&#xff0c;符合“f”作为前缀的&#xff0c;有“furthest”和“front”&#xff0c;按字典序排序并在单词间添加空格后输出&#xff0c;结果为“front furthest”。</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>简单的逻辑题&#xff0c;应该是主要考察数组去重&#xff0c;数组字典排序&#xff0c;过滤等知识</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const str &#61; lines[0];
    const pre &#61; lines[1];

    console.log(lenovo(str, pre));

    lines.length &#61; 0;
  }
});

function lenovo(str, pre) {
  const cache &#61; [...new Set(str.split(/[^a-zA-z]/))];

  const res &#61; cache.filter((ele) &#61;&gt; ele.startsWith(pre)).sort();

  if (res.length &#61;&#61;&#61; 0) return pre;
  return res.join(&#34; &#34;);
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Collections;
import java.util.Scanner;
import java.util.StringJoiner;
import java.util.TreeSet;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String str &#61; sc.nextLine();
    String pre &#61; sc.nextLine();
    System.out.println(getResult(str, pre));
  }

  public static String getResult(String str, String pre) {
    String[] tmp &#61; str.split(&#34;[^a-zA-Z]&#34;);
    TreeSet&lt;String&gt; cache &#61; new TreeSet&lt;&gt;();
    Collections.addAll(cache, tmp);

    StringJoiner sj &#61; new StringJoiner(&#34; &#34;);
    cache.stream().filter(s -&gt; s.startsWith(pre)).forEach(s -&gt; sj.add(s));

    String ans &#61; sj.toString();
    if (ans.length() &gt; 0) {
      return ans;
    } else {
      return pre;
    }
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python">import re

# 输入获取
s &#61; input()
pre &#61; input()


# 算法入口
def getResult(s, pre):
    tmp &#61; re.split(&#34;[^a-zA-Z]&#34;, s)
    cache &#61; list(set(tmp))
    cache.sort()
    cache &#61; list(filter(lambda x: x.startswith(pre), cache))
    if len(cache) &gt; 0:
        return &#34; &#34;.join(cache)
    else:
        return pre


# 算法调用
print(getResult(s, pre))
</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>